package team.scau.laboratory_repair.mobile;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 用于推送微信开发测试账号的菜单选项
 * 返回成功：{"errcode":0,"errmsg":"ok"}
 */
public class PostMenu {

    public static void main(String[] args) throws Exception {

        //微信菜单推送地址

        String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
        //获取access_token
        String accessToken = getAccessToken();
        custmMenuUrl = custmMenuUrl + accessToken;
        URL url = new URL(custmMenuUrl);
        //建立连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        //发送菜单字符流
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(getMenuStr().getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
        //接收返回信息
        InputStream inputStream = connection.getInputStream();
        int size = inputStream.available();
        byte[] bs = new byte[size];
        inputStream.read(bs);
        String message = new String(bs, "UTF-8");
        System.out.println(message);
    }

    public static String getMenuStr() throws JSONException, Exception {

        JSONObject menu = new JSONObject();//一级菜单对象
        JSONArray list = new JSONArray();//一级菜单列表
        //一级菜单内容（一级菜单最多允许3）
        JSONObject subButton = new JSONObject();

        //一级菜单内容的二级菜单列表
        JSONArray subList = new JSONArray();

        //一级菜单内容2的二级菜单内容2
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("type", "view");
        jsonObject.put("name", "进入系统");
        jsonObject.put("url", "http://47.106.110.191:8080/mobile/start/HomePage");
        subList.put(jsonObject);

        subButton.put("name", "维修系统");
        subButton.put("sub_button", subList);

        list.put(subButton);

        menu.put("button", list);
        return menu.toString();
    }

    public static String getAccessToken() throws Exception {

//获取accss_token的地址
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa682f74b488204a2&secret=0eb5bf5727d80e626c0cc801d4e30876";

        URL url = new URL(accessTokenUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");

        connection.setDoOutput(true);

        connection.setDoInput(true);

        connection.connect();
        //获取返回的字符
        InputStream inputStream = connection.getInputStream();

        int size = inputStream.available();

        byte[] bs = new byte[size];

        inputStream.read(bs);

        String message = new String(bs, "UTF-8");

        //获取access_token

        JSONObject jsonObject = new JSONObject(message);

        return jsonObject.getString("access_token");

    }
}
